package com.orgcent.activities;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.Service;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.orgcent.rss.RSSHandler;
import com.orgcent.rss.entity.Channel;
import com.orgcent.rss.entity.Item;
import com.orgcent.util.CharsetHelper;

/**
 * 使用SAX方式解析rss中的xml
 */
public class OnlineParseXML extends Activity {

	private static final String TAG = OnlineParseXML.class.getSimpleName();

	private TextView result_tv;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parse_rss);

		result_tv = (TextView) findViewById(R.id.result_tv);
	}

	public void btnOnClick(View view) {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Service.CONNECTIVITY_SERVICE);
		// 是否可以联网
		NetworkInfo ninfo = cm.getActiveNetworkInfo();
		// 网络可用
		if (ninfo.isAvailable()) {
			RSSHandler handler = new RSSHandler();
			// handler.parse(getContent("http://www.cnbeta.com/backend.php"));
			result_tv.setText("");

			// String uri = "http://www.cnbeta.com/backend.php";
			String uri = "http://www.cnbeta.com/commentrss.php";
			// String uri = "http://webmaster.javaeye.com/rss";

			long beforeFreeMemory = Runtime.getRuntime().freeMemory();
			// handler.parseFromURI(uri);
			handler.parse(getContent(uri));

			result_tv.append("使用内存：");
			result_tv.append(String.valueOf(beforeFreeMemory
					- Runtime.getRuntime().freeMemory()));

			Channel channel = (Channel) RSSHandler.hash.get(Channel.NAME);

			result_tv.append("bytes\n\nRSS: \n标题：");
			result_tv.append(channel.title);
			result_tv.append("\n简介：");
			result_tv.append(channel.description);
			result_tv.append("\n--------------\n消息：");

			for (Item item : channel.items) {
				result_tv.append("\n标题：");
				result_tv.append(CharsetHelper.ucs2ToStr(item.title));
				result_tv.append("\n--------------\n");
			}
		} else {
			result_tv.setText("网络不可用");
		}
	}

	// 获取网络内容
	private String getContent(String uri) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(uri);
		HttpResponse resp;
		StringBuilder content = new StringBuilder();
		try {
			resp = client.execute(get);
			HttpEntity entity = resp.getEntity();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					entity.getContent(), "gbk"));
			String line;
			while ((line = reader.readLine()) != null) {
				content.append(line);
				Log.i(TAG, "line : " + line);
			}
			reader.close();
			entity = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}

		return content.toString();
	}
}
